//
//  WBStatusPictureView.swift
//  WeiBo
//
//  Created by 韩菲菲 on 16/8/14.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

import UIKit

//cell的间距和宽高
private let cellWH: Int = 80
private let margin: Int = 5

class WBStatusPictureView: UICollectionView {
    
    
    //数据源属性
    var pictureInfo: [WBPictureInfo]?{
    
        didSet{
            
            contentLabel.text = "\(pictureInfo?.count ?? 0)"
        
            let size = getPictureViewSize(pictureInfo?.count ?? 0)
            
            //更新约束
            self.snp_updateConstraints { (make) in
             
                make.size.equalTo(size)
                
            }
            
            //刷新约束
            reloadData()
        }

    }
    

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setupUI()
    }
    
    //根据传进来的cell的个数判断控件的大小
    func getPictureViewSize(count: Int) -> CGSize {
        
        //计算行数和列数
        let col = count == 4 ? 2 : count > 3 ? 3 : count
        let row = (count - 1)/3 + 1
        
        //计算宽高
        let w = col * cellWH + (col - 1) * margin
        let h = row * cellWH + (row - 1) * margin
        
        return CGSize(width: CGFloat(w), height: CGFloat(h))
        
    }
    
    
    
    //设置UI
    private func setupUI(){
        
        //显示随机颜色
        backgroundColor = UIColor.clearColor()
        
        //添加控件
        addSubview(contentLabel)
        
        //设置约束
        contentLabel.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        //注册cell类
        self.registerClass(WBPictureViewCell.self, forCellWithReuseIdentifier: "item")
        
        //设置代理
        self.dataSource = self
        self.delegate = self
        
        //布局设置
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = CGSizeMake(CGFloat(cellWH),CGFloat(cellWH))
        
        layout.minimumLineSpacing = CGFloat(margin)
        layout.minimumInteritemSpacing = CGFloat(margin)
    
    }
    
    //懒加载contentLabel
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.redColor(), textSize: 15)
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension WBStatusPictureView: UICollectionViewDataSource,UICollectionViewDelegate{

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureInfo?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("item", forIndexPath: indexPath) as! WBPictureViewCell
        
        
        cell.pictureUrl = pictureInfo![indexPath.item]
        
        return cell
        
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        let photoBrowser = SDPhotoBrowser()
        
        photoBrowser.delegate = self
        
        photoBrowser.currentImageIndex = indexPath.item
        
        photoBrowser.imageCount = self.pictureInfo!.count
        
        photoBrowser.sourceImagesContainerView = self
        
        photoBrowser.show()
  
        
    }

    
}

extension WBStatusPictureView:SDPhotoBrowserDelegate{
    
    func photoBrowser(browser: SDPhotoBrowser!, placeholderImageForIndex index: Int) -> UIImage! {
        
        
        let cell = self.cellForItemAtIndexPath(NSIndexPath(forItem: index, inSection: 0)) as! WBPictureViewCell
        
        return cell.bgImageView.image
    }
    
    func photoBrowser(browser: SDPhotoBrowser!, highQualityImageURLForIndex index: Int) -> NSURL! {
        
        let urlStr = (self.pictureInfo![index].thumbnail_pic ?? "" as NSString).stringByReplacingOccurrencesOfString("thumbnail", withString: "bmiddle")
        
        return NSURL(string: urlStr)
    }
    


}


class WBPictureViewCell: UICollectionViewCell {
    
    var pictureUrl: WBPictureInfo?{
    
        didSet{
        
            bgImageView.wb_setImageWithString(pictureUrl?.thumbnail_pic, placeholderImageName: "weibo_placeholder")
    
            if let pic = pictureUrl?.thumbnail_pic where pic.hasSuffix(".gif"){
            
                //显示Gif图片
                gifImageView.hidden = false
            
            }else
            {
            
                gifImageView.hidden = true
            }
         
        }
    
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
        
    }
    func setupUI() {
        
        //设置白色
        backgroundColor = UIColor.whiteColor()
        
        //添加控件 
        contentView.addSubview(bgImageView)
        contentView.addSubview(gifImageView)
        
        //设置约束
        bgImageView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        gifImageView.snp_makeConstraints { (make) in
            
            make.trailing.bottom.equalTo(bgImageView)
            
        }
        
    }
    
    private lazy var gifImageView: UIImageView = UIImageView(imgName: "timeline_image_gif");
    
    
    private lazy var bgImageView: UIImageView = {
        
        let bgImageView = UIImageView(image: UIImage(named: "weibo_placeholder"))
        
        bgImageView.clipsToBounds = true
        
        bgImageView.contentMode = .ScaleAspectFill
    
        return bgImageView
    
    }()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
}






